﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meechisoft.TFS.BranchIndicator
{
    using Microsoft.TeamFoundation.Framework.Client;
    using System.Windows.Media;

    public static class TfsRegistryExtensions
    {

        public static void SetBranchColours(this ITeamFoundationRegistry registry, BranchSetting branch)
        {
            registry.SetUserValue(String.Format("Branch-{0}-PrimaryColor", branch.ID), branch.PrimaryColor.ToString());
            registry.SetUserValue(String.Format("Branch-{0}-SecondaryColor", branch.ID), branch.SecondaryColor.ToString());
            registry.SetUserValue(String.Format("Branch-{0}-Gradient", branch.ID), branch.Gradient);
        }

        public static void PopulateBranchColours(this ITeamFoundationRegistry registry, BranchSetting branch)
        {
            var s_pc = registry.GetUserValue<string>(String.Format("/Branch-{0}-PrimaryColor", branch.ID), null);
            var s_sc = registry.GetUserValue<string>(String.Format("/Branch-{0}-SecondaryColor", branch.ID), null);

            var pc = Colors.Transparent;
            var sc = Colors.Transparent;

            if (!String.IsNullOrWhiteSpace(s_pc))
                pc = (Color)ColorConverter.ConvertFromString(s_pc);

            if (!String.IsNullOrWhiteSpace(s_sc))
                sc = (Color)ColorConverter.ConvertFromString(s_sc);

            branch.PrimaryColor = pc;
            branch.SecondaryColor = sc;
            branch.Gradient =  registry.GetUserValue(String.Format("/Branch-{0}-Gradient", branch.ID), false);
        }
    }
}
